#!/bin/bash

# Auto-run script for database demo
echo "Starting database demo..."

cd /Users/zhangpeng/Desktop/150/storage_system_cpp

# Create a simple working version
cat > working_demo.cpp << 'EOF'
#include <iostream>
#include <string>
#include <vector>

struct Record {
    int id;
    std::string data;
    Record(int i, const std::string& d) : id(i), data(d) {}
};

struct Table {
    std::string name;
    std::vector<Record> records;
    Table(const std::string& n) : name(n) {}
};

class Database {
private:
    std::vector<Table> tables;
public:
    void createTable(const std::string& name) {
        tables.push_back(Table(name));
        std::cout << "Table '" << name << "' created successfully!" << std::endl;
    }

    void insertRecord(const std::string& tableName, int id, const std::string& data) {
        for (auto& table : tables) {
            if (table.name == tableName) {
                table.records.push_back(Record(id, data));
                std::cout << "Record inserted: ID=" << id << ", Data='" << data << "'" << std::endl;
                return;
            }
        }
        std::cout << "Table not found!" << std::endl;
    }

    void showRecords(const std::string& tableName) {
        for (const auto& table : tables) {
            if (table.name == tableName) {
                std::cout << "Records in table '" << tableName << "':" << std::endl;
                for (const auto& record : table.records) {
                    std::cout << "  ID: " << record.id << ", Data: " << record.data << std::endl;
                }
                return;
            }
        }
        std::cout << "Table not found!" << std::endl;
    }
};

int main() {
    std::cout << "🚀 Simple Database Demo" << std::endl;
    std::cout << "=====================" << std::endl;

    Database db;

    // Auto-execute the demo
    std::cout << "1. Creating table 'user'..." << std::endl;
    db.createTable("user");

    std::cout << "2. Inserting record (alice, 18)..." << std::endl;
    db.insertRecord("user", 1, "alice,18");

    std::cout << "3. Showing all records..." << std::endl;
    db.showRecords("user");

    std::cout << "✅ Demo completed successfully!" << std::endl;
    return 0;
}
EOF

echo "Compiling working demo..."
g++ -std=c++17 -o working_demo working_demo.cpp

echo "Running demo..."
./working_demo

echo "Demo finished."
